package com.web.dao;

import com.github.pagehelper.Page;
import com.web.controller.web.WebRechargeController;
import com.web.entity.RechargeRecord;
import org.apache.ibatis.annotations.*;
import org.apache.ibatis.type.JdbcType;
import org.springframework.security.access.method.P;

import java.math.BigDecimal;
import java.util.List;

/**
 * @author FDjavaone
 */
public interface RechargeRecordMapper {
    @Delete({
        "delete from t_recharge_record",
        " where id = #{id,jdbcType=BIGINT}"
    })
    int deleteByPrimaryKey(Long id);

    @Insert({
        "insert into t_recharge_record (out_trade_no,name,phone, type, ",
        " amount, m_id, status, ",
        " create_time, modify_time)",
        " values (#{outTradeNo,jdbcType=VARCHAR},#{name,jdbcType=VARCHAR},#{phone,jdbcType=VARCHAR}, #{type,jdbcType=INTEGER}, ",
        " #{amount,jdbcType=DECIMAL}, #{mId,jdbcType=BIGINT}, #{status,jdbcType=INTEGER}, ",
        " #{createTime,jdbcType=TIMESTAMP}, #{modifyTime,jdbcType=TIMESTAMP})"
    })
    @SelectKey(statement="SELECT LAST_INSERT_ID()", keyProperty="id", before=false, resultType=Long.class)
    int insert(RechargeRecord record);

    @InsertProvider(type=RechargeRecordSqlProvider.class, method="insertSelective")
    @SelectKey(statement="SELECT LAST_INSERT_ID()", keyProperty="id", before=false, resultType=Long.class)
    int insertSelective(RechargeRecord record);

    @Select({
        "select",
        " id, out_trade_no,name,phone, type, amount, m_id, status, create_time, modify_time",
        " from t_recharge_record",
        " where id = #{id,jdbcType=BIGINT}"
    })
    @Results({
        @Result(column="id", property="id", jdbcType=JdbcType.BIGINT, id=true),
        @Result(column="out_trade_no", property="outTradeNo", jdbcType=JdbcType.VARCHAR),
        @Result(column="name", property="name", jdbcType=JdbcType.VARCHAR),
        @Result(column="phone", property="phone", jdbcType=JdbcType.VARCHAR),
        @Result(column="type", property="type", jdbcType=JdbcType.INTEGER),
        @Result(column="amount", property="amount", jdbcType=JdbcType.DECIMAL),
        @Result(column="m_id", property="mId", jdbcType=JdbcType.BIGINT),
        @Result(column="status", property="status", jdbcType=JdbcType.INTEGER),
        @Result(column="create_time", property="createTime", jdbcType=JdbcType.TIMESTAMP),
        @Result(column="modify_time", property="modifyTime", jdbcType=JdbcType.TIMESTAMP)
    })
    RechargeRecord selectByPrimaryKey(Long id);

    @UpdateProvider(type=RechargeRecordSqlProvider.class, method="updateByPrimaryKeySelective")
    int updateByPrimaryKeySelective(RechargeRecord record);

    @Update({
        "update t_recharge_record",
        " set out_trade_no = #{outTradeNo,jdbcType=VARCHAR},",
          " type = #{type,jdbcType=INTEGER},",
          " name = #{name,jdbcType=VARCHAR},",
          " phone = #{phone,jdbcType=VARCHAR},",
          " amount = #{amount,jdbcType=DECIMAL},",
          " m_id = #{mId,jdbcType=BIGINT},",
          " status = #{status,jdbcType=INTEGER},",
          " create_time = #{createTime,jdbcType=TIMESTAMP},",
          " modify_time = #{modifyTime,jdbcType=TIMESTAMP}",
        " where id = #{id,jdbcType=BIGINT}"
    })
    int updateByPrimaryKey(RechargeRecord record);

    @Select({
            "select",
            "id, out_trade_no, type,name,phone, amount, m_id, status, create_time, modify_time",
            "from t_recharge_record",
            "where out_trade_no = #{orderNum,jdbcType=VARCHAR}"
    })
    @Results({
            @Result(column="id", property="id", jdbcType=JdbcType.BIGINT, id=true),
            @Result(column="out_trade_no", property="outTradeNo", jdbcType=JdbcType.VARCHAR),
            @Result(column="name", property="name", jdbcType=JdbcType.VARCHAR),
            @Result(column="phone", property="phone", jdbcType=JdbcType.VARCHAR),
            @Result(column="type", property="type", jdbcType=JdbcType.INTEGER),
            @Result(column="amount", property="amount", jdbcType=JdbcType.DECIMAL),
            @Result(column="m_id", property="mId", jdbcType=JdbcType.BIGINT),
            @Result(column="status", property="status", jdbcType=JdbcType.INTEGER),
            @Result(column="create_time", property="createTime", jdbcType=JdbcType.TIMESTAMP),
            @Result(column="modify_time", property="modifyTime", jdbcType=JdbcType.TIMESTAMP)
    })
    RechargeRecord selectByOutTradeNo(@Param("orderNum") String orderNum);

    @SelectProvider(type = RechargeRecordSqlProvider.class, method = "selectAllRechages")
    @Results({
            @Result(column="id", property="id", jdbcType=JdbcType.BIGINT, id=true),
            @Result(column="out_trade_no", property="outTradeNo", jdbcType=JdbcType.VARCHAR),
            @Result(column="name", property="name", jdbcType=JdbcType.VARCHAR),
            @Result(column="phone", property="phone", jdbcType=JdbcType.VARCHAR),
            @Result(column="type", property="type", jdbcType=JdbcType.INTEGER),
            @Result(column="amount", property="amount", jdbcType=JdbcType.DECIMAL),
            @Result(column="m_id", property="mId", jdbcType=JdbcType.BIGINT),
            @Result(column="status", property="status", jdbcType=JdbcType.INTEGER),
            @Result(column="create_time", property="createTime", jdbcType=JdbcType.TIMESTAMP),
            @Result(column="modify_time", property="modifyTime", jdbcType=JdbcType.TIMESTAMP)
    })
    Page<RechargeRecord> selectAllRechages(WebRechargeController.RechargeColumns rechargeColumns);

    @Select({
            "select",
            " id, out_trade_no, type,name,phone, amount, m_id, status, create_time, modify_time",
            " from t_recharge_record",
            " where status=0  and create_time <= date_add(now(), interval - 5 MINUTE)"
    })
    @Results({
            @Result(column="id", property="id", jdbcType=JdbcType.BIGINT, id=true),
            @Result(column="out_trade_no", property="outTradeNo", jdbcType=JdbcType.VARCHAR),
            @Result(column="name", property="name", jdbcType=JdbcType.VARCHAR),
            @Result(column="phone", property="phone", jdbcType=JdbcType.VARCHAR),
            @Result(column="type", property="type", jdbcType=JdbcType.INTEGER),
            @Result(column="amount", property="amount", jdbcType=JdbcType.DECIMAL),
            @Result(column="m_id", property="mId", jdbcType=JdbcType.BIGINT),
            @Result(column="status", property="status", jdbcType=JdbcType.INTEGER),
            @Result(column="create_time", property="createTime", jdbcType=JdbcType.TIMESTAMP),
            @Result(column="modify_time", property="modifyTime", jdbcType=JdbcType.TIMESTAMP)
    })
    List<RechargeRecord> selectByStatus();

    @Select({
            "select",
            " sum(amount)",
            " from t_recharge_record",
            " where status=1  and m_id= #{id,jdbcType=BIGINT}"
    })
    BigDecimal countRecharegeByMid(Long id);
}